package com.xmb.common.service.impl;

import com.xmb.common.service.SnowflakeIdWorker;
import com.xmb.common.utils.SNIDWorker;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Created by lyndon on 2019/3/26.
 */
@Service
public class SnowflakeIdWorkerImpl implements SnowflakeIdWorker{

    /**
     * 雪花算法节点机器id
     */
    @Value("${snowflake.work-id}")
    private Long workId;

    /**
     * 雪花算法数据中心id
     */
    @Value("${snowflake.datacenter-id}")
    private Long datacenterId;

    private SNIDWorker snidWorker;

    @Override
    public Long getNextId() {
        synchronized (this) {
            if(snidWorker == null){
                snidWorker = new SNIDWorker(workId,datacenterId);
            }
        }
        return snidWorker.nextId();
    }
}
